#include "PlayerProjectileFactory.h"
#include "..\Generic\IntMessage.h"
#include "..\Generic\MessageSystem.h"
#include "..\Components\AnimationComponent.h"
#include "..\Components\PhysicsComponent.h"
#include "..\Components\VisualComponent.h"
#include "..\Components\CollisionComponent.h"
#include "..\Components\DamageComponent.h"
#include "..\Components\SensorComponent.h"
#include "..\Managers\RenderManager.h"
#include <functional>


PlayerProjectileFactory::~PlayerProjectileFactory(){}

GameObject* PlayerProjectileFactory::createProjectile(const vn::Vector2& p_position, const vn::Vector2& p_velocity)
{
	std::function<void()> cTorCallBack = [] {
		IntMessage msg(1);
		MessageSystem::getSingleton().notify("bullet", &msg); 
	};
	std::function<void()> dTorCallBack = [] { 
		IntMessage msg(-1);
		MessageSystem::getSingleton().notify("bullet", &msg); 
	};


	GameObject* gameObject = new GameObject("player_bullet", nullptr, cTorCallBack);
	gameObject->setDTorCallBack(dTorCallBack);

	IComponent* component = new VisualComponent(1, "mm_bullet", 1.1f);
	gameObject->addComponent(component);
	component = new PhysicsComponent(b2_dynamicBody, b2Vec2(10.0f, 10.0f), b2Vec2(p_position.x, p_position.y), FIXT_ID_PLAYER_PROJECTILE, COLLF_PLAYER_BULLET, COLLF_WORLD, 0.0f);
	static_cast<PhysicsComponent*>(component)->getBody()->SetLinearVelocity(b2Vec2(p_velocity.x, p_velocity.y));
	static_cast<PhysicsComponent*>(component)->getBody()->SetGravityScale(0);
	static_cast<PhysicsComponent*>(component)->getBody()->SetFixedRotation(true);
	gameObject->addComponent(component);
	component = new AnimationComponent("animations/rockman_projectile.xml", "bullet");
	gameObject->addComponent(component);

	component = new DamageComponent(2);
	gameObject->addComponent(component);

	component = new SensorComponent(FIXT_ID_PLAYER_PROJECTILE_SENSOR, b2Vec2(10.0f, 10.0f), b2Vec2(0.0f, 0.0f), 0.0f, COLLF_PLAYER_BULLET_SENSOR, COLLF_ENEMY_SENSOR);
	gameObject->addComponent(component);

	std::function<void()> func = [=] { gameObject->removeMe(); };
	component = new CollisionComponent(func, RenderManager::getSingleton().getRenderEngine()->getCamera("main"));
	gameObject->addComponent(component);
	gameObject->initComponents();

	return gameObject;
}